package com.zhentao.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.common.Result;
import com.zhentao.domain.UseBattery;
import com.zhentao.domain.vo.UseBatteryVO;
import com.zhentao.service.UseBatteryService;
import com.zhentao.mapper.UseBatteryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author 86133
 * @description 针对表【use_battery】的数据库操作Service实现
 * @createDate 2024-09-06 10:00:56
 */
@Service
public class UseBatteryServiceImpl extends ServiceImpl<UseBatteryMapper, UseBattery>
        implements UseBatteryService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Result<UseBatteryVO> getUseList() {
        String s = stringRedisTemplate.opsForValue().get("useBattery");
        List<UseBattery> useBatteries = JSON.parseArray(s, UseBattery.class);
        HashSet<Date> dateList = new HashSet<>();
        for (UseBattery useBattery : useBatteries) {
            dateList.add(useBattery.getCreateTime());
        }

        UseBatteryVO useBatteryVO = getUseBatteryVO(dateList, useBatteries);
        return Result.build(useBatteryVO, 200, "操作成功");
    }

    private static UseBatteryVO getUseBatteryVO(HashSet<Date> dateList, List<UseBattery> useBatteries) {
        List<Date> dateList1 = new ArrayList<>(dateList);
        Collections.sort(dateList1);
        List<Integer> useList = new ArrayList<>();
        int i = 0;
        for (Date date : dateList1) {
            i=0;
            for (UseBattery use : useBatteries) {
                if (date.equals(use.getCreateTime())) {
                    i++;
                }
            }
            useList.add(i);
        }

        UseBatteryVO useBatteryVO = new UseBatteryVO();

        useBatteryVO.setDateList(dateList1);
        useBatteryVO.setUseList(useList);
        return useBatteryVO;
    }
}




